import { getIndustryList } from '@/api/Plan';
import { Cascader } from 'antd';
import { useEffect, useState } from 'react';
interface Option {
  value: string | number;
  label: string;
  children?: Option[];
}
export function TradeCascaderSelect(props: { placeholder?: string; onChange?: any }) {
  const { placeholder = '', onChange } = props;
  const [options, setOption] = useState<Option[]>([]);
  useEffect(() => {
    getIndustryList({}).then((value: any) => {
      const list: Option[] = [];
      value.map(
        (v: { tradeDict: { tradeDictCode: string; tradeDictName: string }[]; entTypeCode: string; name: string }) => {
          if (v.entTypeCode === 'consignor') {
            ////货主的行业分类不展示
            v.tradeDict = [];
          } else {
            v.tradeDict = v.tradeDict ? v.tradeDict : [];
          }
          const children = v.tradeDict.map((t) => {
            return {
              value: t.tradeDictCode,
              label: t.tradeDictName,
            };
          });
          const obj = {
            value: v.entTypeCode,
            label: v.name,
            children: children,
          };
          list.push(obj);
        },
      );

      setOption(list.filter((v) => v.children && v.children.length));
    });
  }, []);

  const onChangeCasCader = (value?: any) => {
    if (!value) {
      onChange && onChange(undefined);
    } else {
      onChange && onChange(value[1] || '');
    }
  };

  return <Cascader options={options} allowClear onChange={onChangeCasCader} placeholder={placeholder} />;
}
